package com.itheima.studentmanager;

import com.itheima.domain.Student;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/update")
public class UpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Object id = request.getSession().getAttribute("id");
        String i = (String)id;
        int b = Integer.parseInt(i);
        Map<String, String[]> map = request.getParameterMap();
        Student stu = new Student();
        try {
            BeanUtils.populate(stu,map);
        } catch (Exception e) {}
        StudentDao dao = new StudentDao();
        dao.update(b,stu);
        request.getRequestDispatcher("/findAll").forward(request,response);
    }
}
